<?php

if (!defined('ROOTPATH')) {
	die(-1);
}

require_once INCLUDESPATH . 'db/DBConnection.php';
require_once INCLUDESPATH . 'db/User.php';

/**
 *
 *
 * @author hutushen222 2010-08-08
 * @version 1.0.0
 */
class UserDAO {

	/**
	 * the default constructor of the user dao
	 *
	 * @author hutushen222
	 */
	public function UserDAO() {

	}

	/**
	 * insert an user's info into the database as a record of the User table.
	 *
	 * @param User $user -- an user object
	 * @author hutushen222 2010-08-08
	 * @return true -- if successful
	 * @see manipulate
	 */
	public function insert(User $user) {
		$sql = "insert into `user` (username, nickname, password, email, " .
		"realname, gender, photo, birthday, registertime, role, question, " . 
		"answer) values ('" .		
		$user->getUsername() . "', '" .
		$user->getNickname() . "', '" .
		$user->getPassword() . "', '" .
		$user->getEmail() . "', '" .
		$user->getRealname()	 . "', '" .
		$user->getGender() . "', '" .
		$user->getPhoto() . "', '" .
		$user->getBirthday() . "', '" .
		$user->getRegisterTime() . "', '" .
		$user->getRole() . "', '" .
		$user->getQuestion() . "', '" .
		$user->getAnswer() . "'" .
		")";		

		return $this->manipulate($sql);
	}

	/**
	 * update the user's info in the database record
	 *
	 * @param User $user -- an user object
	 * @author hutushen222 2010-08-09
	 * @return true -- if successful
	 * @see manipulate
	 */
	public function update(User $user) {
		$sql = "update `user` set " .
		"nickname = '" . $user->getNickname() . "', " .
		"password = '" . $user->getPassword() . "', " . 
		"realname = '" . $user->getRealname() . "', " .
		"photo = '" . $user->getPhoto() . "', " .
		"gender = '" . $user->getGender() . "', " .
		"birthday = '" . $user->getBirthday() . "', " .
		"role = '" . $user->getRole() . "', " .
		"question = '" . $user->getQuestion() . "', " . 
		"answer = '" . $user->getAnswer() . "' " . 
		"where id = '" . $user->getId() . "'";

		return $this->manipulate($sql);
	}

	/**
	 * delete an user from the database whit the given id
	 *
	 * @param int $id -- an id relative with a record in the database
	 * @author hutushen222 2010-08-08
	 * @return true -- if successful
	 * @see manipulate
	 */
	public function delete($id) {
		$sql = 'delete from `user` where id = ' . $id;
		return $this->manipulate($sql);
	}

	/**
	 * get an user object from the database with the given id
	 *
	 * @param int $id -- an id relative with a record in the database
	 * @author hutushen222 2010-08-09
	 * @return User user -- an user object
	 * @see retrieve
	 */
	public function retrieveById($id) {
		$sql = "select * from `user` where id = " .  $id;
		$users = $this->retrieve($sql);
		return $users[0];
	}

	/**
	 * get the users from the database with the given property and its value
	 *
	 * @param string $property
	 * @param string $value
	 * @param string $orderby -- a column name of the word table
	 * @author hutushen222 2010-08-09
	 * @return array Users -- an array of user objects
	 * @see retrieve
	 */
	public function retrieveByProperty($property, $value, $orderby = "id") {
		$sql = "select * from `user` where " . $property . " = '" . $value .
		"' order by " . $orderby;
		return $this->retrieve($sql);
	}

	/**
	 * get the user objects from the databases with the given properties and
	 * their values
	 *
	 * @param array $properties
	 * @param array $values
	 * @param string $orderby -- a column name of the word table
	 * @author hutushen222 2010-08-09
	 * @return array Users -- an array of user objects
	 * @see retrieve
	 */
	public function retrieveByProperties(array $properties, $orderby = "id",
	$order = 1,  $page = 0, $usersperpage = 21) {
		# manipulate the sql statement
		$sql = "select * from `user` where ";

		foreach($properties as $key => $value) {
			$sql .= '`' . $key . '` = \'' . $value . '\' and ';
		}
		$sql = substr($sql, 0, -4);

		if ($order == 1) {
			$sql .= ' order by ' . $orderby . ' asc';
		} else {
			$sql .= ' order by ' . $orderby . ' desc';
		}

		if ($page) {
			$sql .= ' limit ' . ($usersperpage * ($page - 1)) . ', ' . $usersperpage;
		}

		return $this->retrieve($sql);
	}

	/**
	 * get all the records of user in the database
	 *
	 * @param string $orderby -- a column name of the user table
	 * @author hutushen222 2010-08-09
	 * @return array Users -- an array of user objects
	 * @see retrieve
	 */
	public function retrieveAll($orderby = "id") {
		$sql = "select * from `user` order by " . $orderby;
		return $this->retrieve($sql);
	}

	/**
	 * manipulate the dababase for update the info in the table
	 *
	 * For INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns
	 * TRUE on success or FALSE on error.
	 *
	 * @param string $sql -- a query string
	 * @author hutushen222 2010-08-09
	 * @return true -- if successful
	 */
	private function manipulate($sql) {
		$dbc = new DBConnection();
		$dbc->connectAIO();
		$dbc->query($sql);
		$dbc->close();
		return true;
	}

	/**
	 * query in the database for the relative records
	 *
	 * For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements
	 * returning resultset, mysql_query() returns a resource on success,
	 * or FALSE on error.
	 *
	 * @param string $sql -- a query string
	 * @author hutushen222 2010-08-09
	 * @return array Users -- an array of user objects
	 */
	private function retrieve($sql) {
		$dbc = new DBConnection();
		$dbc->connectAIO();

		$result = $dbc->query($sql);

		if ($dbc->affectedRows() == 0) {
			return array();
		}

		$users = array();
		while ($row = mysql_fetch_assoc($result)) {
			# initialize a user with the data from the database
			$user = $this->initUser($row['id'], $row['username'], $row['nickname'],
			$row['password'], $row['email'], $row['realname'], $row['photo'],
			$row['gender'], $row['birthday'], $row['registertime'], $row['role'],
			$row['question'], $row['answer']);

			$users[] = $user;
		}

		$dbc->close();

		return $users;
	}

	/**
	 * initialize an user with these parameters.
	 * you must pay attention on the parameters' order.
	 *
	 * @author hutushen222 2010-08-08
	 * @return User user -- an user object
	 */
	private function initUser() {
		$properties = func_get_args();

		if (count($properties) < 13) {
			return null;
		}

		$user = new User();
		$user->setId($properties[0]);
		$user->setUsername($properties[1]);
		$user->setNickname($properties[2]);
		$user->setPassword($properties[3]);
		$user->setEmail($properties[4]);

		$user->setRealname($properties[5]);
		$user->setPhoto($properties[6]);
		$user->setGender($properties[7]);
		$user->setBirthday($properties[8]);

		$user->setRegisterTime($properties[9]);
		$user->setRole($properties[10]);

		$user->setQuestion($properties[11]);
		$user->setAnswer($properties[12]);
		return $user;
	}

}
?>